package FilesGenerator

import java.io._
import javax.swing.ImageIcon
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
object FileManager {

   // private method to create a directory  with array of folders
   def createDirectory(splitedPath: Array[String]): Boolean = {

      var currentPath = ""
      var created = true

      // go through all folders
      for (str <- splitedPath
           if created == true) {

         currentPath += str + '/'

         var folder = new java.io.File(currentPath)

         // if folder don't exist, it will be created
         if (!(folder.exists)) {
            created = folder.mkdir()
         }
      }
      created
   }

   // private method to create a directory with it absolute path
   def createDirectory(path: String): Boolean = {
      createDirectory(path.split("/"))
   }


   // private method to create a directory with sub-directories in package form, means : folder1.folder2.forlder3
   def createDirectory(path: String, pkg: String): Boolean = {
      createDirectory((path.split('/') ++ pkg.split('.')).mkString("/"))
   }

   // Create a empty file in the specific path with the specific name. Return the path of file, or a empty string if the file cannot be created
   def createFile(path: String, nameOfFile: String): String = {
      createFile(path + "/" + nameOfFile)
   }

   // Create a empty file in the specific path with the specific name. Return the path of file, or a empty string if the file cannot be created
   def createFile(filePath: String): String = {

      val path = filePath.split("/").init.mkString("/")

      var created = false

      // ensure that folder exist
      created = createDirectory(path)

      // create the file if the folder exist
      if (created == true) {
         // Create the file
         new PrintStream(new FileOutputStream(filePath)).close

         // Check if the file exist
         created = new java.io.File(filePath).exists
      }

      // return the path of the file if it exist, otherwise return empty string
      if (created == true) {
         filePath
      }
      else {
         ""
      }
   }

   // Write the textToWrite in the specific file
   def writeInFile(pathOfFile: String, textToWrite: String): Unit = {

      new PrintStream(new FileOutputStream(pathOfFile)).print(textToWrite)

   }

   // Write the textToWrite in the specific file, return false if the folder doesn't exists
   def writeInFile(path: String, nameOfFile: String, textToWrite: String): Boolean = {

      var ok = false

      if (new java.io.File(path).exists) {
         writeInFile(path + "/" + nameOfFile, textToWrite)
         ok = true
      }
      ok
   }

   def makeToolChain(path: String): Unit = {
      //copy the toolchain in project
      copyFile("files/build.xml", path + "/build.xml")
      copyFile("files/build-scala.xml", path + "/build-scala.xml")
   }

   // Copy the content of source file in the destination file,  return false if source file doesn't exists
   def copyFile(srcFile: String, destFile: String): Boolean = {
      val src = new File(srcFile)
      val dest = new File(destFile)

      var ok = false

      //ensure that source file exists
      if (src.exists) {

         // ensure that destination file exists
         if (!dest.exists) {
            createFile(destFile.split("/").init.mkString("/"), destFile.split("/").last)
         }

         val in = new FileInputStream(srcFile).getChannel()
         val out = new FileOutputStream(destFile).getChannel()

         in.transferTo(0, in.size(), out)

         in close()
         out close()

         ok = true
      }
      ok
   }

   def saveImage(path: String, nameOfFile: String, theImage: ImageIcon): Unit = {

      val img = theImage.getImage
      val bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB)
      val g2 = bi.createGraphics()
      g2 drawImage (img, 0, 0, null)
      g2 dispose ()
      ImageIO write (bi, "png", new File(path + "/" + nameOfFile))
   }

}
